﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace RHD.Common.Web.Components.JsonResult {
	public abstract class KeyValuePairResult : ActionResult {
		protected IList<SelectListItem> Items { get; set; }

		public KeyValuePairResult(IEnumerable items, string dataValueField, string dataTextField) {
			this.Items = new SelectList(items, dataValueField, dataTextField).ToList();
		}

		public override void ExecuteResult(ControllerContext context) {
			string json = JsonConvert.SerializeObject(this.Items, new JavaScriptDateTimeConverter());
			context.HttpContext.Response.ContentType = "application/json";
			context.HttpContext.Response.Write(json);
		}
	}
}